<template>
	<view style="padding-top: 100rpx;">
	
		<view class="text-outer" v-for=" (item ,index) in allArr" :key="index" @click="next(item)" v-if="user && item">
			<view class="text-count">{{item.arr.length}}</view>
			 <image :src="user.head_img" mode="" style="width: 60rpx;height: 60rpx;margin-right: 20rpx;" ></image>
			<view> {{filterTime(item.time)}}</view>
			<view>用户 {{item.title}}了</view>
		</view>
			<view v-if="allArr.length==0 && user" style="text-align: center;">暂无数据</view>
		 <view v-if="!user" class="text-tips">
			 <view  style="text-align:center;" >未登录账号请登录后查看消息提醒</view>
			 <button class="login-btn" @tap="login">立即登录</button>
		 </view>
	</view>
</template>

<script>
	import GoEasy from '@/lib/goeasy-2.4.6.min.js';
	export default {
		data() {
			return {
				list: [],
				user:'',
				allArr:[],//统计所有评论收藏
				status:0,
			}
		},
		filters: {
			showDate(value) {
				let date = new Date(value * 1000);
				var time = this.formatDate2(date, 'yyyy-MM-dd hh:mm:ss');
				return time
			}
		},
     
		created() {
		this.user=uni.getStorageSync('user')
		this.getContent();

		},
		
	
		methods: {
			getContent(){
				var that = this;
				this.user=uni.getStorageSync('user')
				const goEasy = GoEasy.getInstance({
					host: "hangzhou.goeasy.io",
					appkey: "BC-08f64abe98b4429fa4bb0345eee146de",
					allowNotification: true,
					modules: ['pubsub'],
				});
				goEasy.connect({
					onSuccess: function() {
					
					},
					onFailed: function(error) {
						console.log("Failed to connect GoEasy, code:" + error.code + ",error:" + error.content);
					},
					onProgress: function(attempts) {
						console.log("GoEasy is connecting", attempts);
					}
				});
				goEasy.pubsub.history({
					channel: that.user.id, //必需项
					start: new Date(new Date(new Date().toLocaleDateString()).getTime()), //可选项，开始时间，默认为0，即从第一条消息开始查
					end: new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 -
						1), //可选项，结束时间,默认为当前时间
					limit: 10, //可选项，返回的消息条数，默认为10条，最多30条
					onSuccess: function(response) {
						var dArr=[];
						var pArr=[];
						var gArr=[];
						var allArr=[];
						var list = JSON.stringify(response.content)
						that.list = JSON.parse(list)
						console.log("that.list",that.list)
						if(that.list.messages){
							that.list.messages.map((item)=>{
								if(item.content.includes('点赞')){
									dArr.push(item)
								}
								if(item.content.includes('评论')){
									pArr.push(item)
								}
								if(item.content.includes('关注')){
									gArr.push(item)
								}
							})
							if(dArr.length){
								var obj={
									title:dArr[0].content,
									time:dArr[0].time,
									arr:dArr
								}
							}
							if(pArr.length){
								var obj2={
									title:pArr[0].content,
									time:pArr[0].time,
									arr:pArr
								}
							}
							if(gArr.length){
								var obj3={
									title:gArr[0].content,
									time:gArr[0].time,
									arr:gArr
								}
							}
							
							if(obj){
							allArr.push(obj)
							}
							if(obj2){
							allArr.push(obj2)
							}
							if(obj3){
							allArr.push(obj3)
							}
						
							that.allArr=allArr
							console.log("that.allArr",that.allArr)
						}
					
				        
					},
					onFailed: function(error) { //获取失败
						console.log("Failed to obtain history, code:" + error.code + ",error:" + error.content);
					},
				});
			},
			login(){
				uni.navigateTo({
					url:'/pages/login/login?status=1'
				})
				this.status=1;
			},
			user() {
				return this.$store.state.user;
			},
			next(item){
				
			uni.navigateTo({
				url:'/pages/video/pushDefault?arr='+JSON.stringify(item.arr)
			})
			},
			//转换时间
		filterTime(time) {
		var date = new Date(+time),
		    Y = date.getFullYear() + '-',
		    M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-',
		    D = date.getDate() + ' ',
		    h = date.getHours() + ':',
		    m = date.getMinutes() + ':',
		    s = date.getSeconds()
		    return Y + M + D   // '2022-05-20'
		  },

			 padLeftZero(str) {
				return ('00' + str).substr(str.length);
			},


		}

	}
</script>

<style>
	
	.login-btn { 
		margin-top: 48rpx;
		font-size: 36rpx;
		width: 80%;
		// background-color: $uni-text-color-active;
		// color: $uni-text-color-inverse;
		    background-color: #fff;
		    color: #333;
		    border-radius: 25px;
		    border: 1px solid #000;
		border-radius: 50rpx;
	}
	.text-outer {
		position: relative;
		width: 100%;
		display: flex;
		align-items: center;
		 /* justify-content: space-between; */
		line-height: 120rpx;
		height: 120rpx;
		padding-left: 30rpx;
		padding-right: 30rpx;
		border-bottom: 1px solid #333;
		box-sizing: border-box;
	}
	.text-count{
		position: absolute;
		z-index: 100;
		left: 10%;
		top: 10%;
		width: 30rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		height: 30rpx;
		border-radius: 50%;
		background-color: red;
		color: #fff;
	}
	.text-tips{
		position: absolute;
		width: 100%;
		top: 50%;
		left: 50%;
		transform:translate(-50%,-50%);
	}
</style>
